package com.example2016.cjj.mycustomviewdemo.MyView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.example2016.cjj.mycustomviewdemo.R;

/**
 * Created by CJJ on 2017/2/23.
 */

public class CustomImageView  extends View{

    private static final String TAG = "CustomImageView";

    //图片缩放模式
    private static final int IMAGE_SCALE_FITXY =0;
    private static final int IMAGE_SCALE_CENTER =1;

    //文字
    private String mTitleText="";

    //文字的大小
    private int mTitleSize = 15;

    //文字的颜色
    private int mTitleTextColor= Color.BLACK;

    //图片缩放模式
    private int mImageScaleType=0;

    //图片
    private Bitmap mImage;

    private Paint mPaint;

    //文本rect
    private Rect mTextBoundRect;

    //view的宽、高
    private int mWidth,mHeight;




    public CustomImageView(Context context) {
        this(context,null);
    }

    public CustomImageView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MyCustomImageView,defStyleAttr,0);
        int n = a.getIndexCount();
        for(int i=0; i< n; i++){
            int attr = a.getIndex(i);
            switch (attr){
                case R.styleable.MyCustomImageView_imageTitleText:
                    mTitleText = a.getString(attr);
                    break;
                case R.styleable.MyCustomImageView_imageTitleSize:
                    mTitleSize = a.getDimensionPixelSize(attr,
                            (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,mTitleSize,getResources().getDisplayMetrics()) );
                    break;
                case R.styleable.MyCustomImageView_imageTitleColor:
                    mTitleTextColor = a.getColor(attr,mTitleTextColor);
                    break;
                case R.styleable.MyCustomImageView_imageSrc:
                    mImage= BitmapFactory.decodeResource( getResources(), a.getResourceId(attr,0) );
                    break;
                case R.styleable.MyCustomImageView_imageScaleType:
                    mImageScaleType = a.getInt(attr,mImageScaleType);
                    break;

            }

        }

        a.recycle();


        mPaint = new Paint();
        mPaint.setFlags(Paint.ANTI_ALIAS_FLAG); //抗锯齿

        mPaint.setTextSize(mTitleSize);
        mTextBoundRect = new Rect();
        mPaint.getTextBounds(mTitleText,0,mTitleText.length(),mTextBoundRect);  //计算了描绘字体所需要的范围


    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        //计算宽
        int widthMode =  MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        switch (widthMode){
            case MeasureSpec.EXACTLY:
                //设置了精确值或者是MATCH_PARENT
                mWidth = widthSize;
                break;
            case MeasureSpec.AT_MOST:
                //限制在一定范围内，一般是WRAP_CONTENT
                //图片的宽度
                int desireByimg = getPaddingLeft() + mImage.getWidth() + getPaddingRight();
                //文字的宽度
                int desireByTxt = getPaddingLeft() + mTextBoundRect.width() + getPaddingRight();
                int  desire = Math.max(desireByimg,desireByTxt);
                mWidth = Math.min(desire,widthSize);
                break;
        }

        //计算高
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        switch (heightMode){
            case MeasureSpec.EXACTLY:
                mHeight = heightSize;
                break;
            case MeasureSpec.AT_MOST:
                int desire = getPaddingLeft() + getPaddingRight() + mImage.getHeight() + mTextBoundRect.height();
                mHeight = Math.min(desire,heightSize);

                break;
        }

        setMeasuredDimension(mWidth,mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //画边框
        mPaint.setStrokeWidth(5);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.CYAN);
        canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mPaint);


        Rect rect = new Rect();
        if(mImageScaleType ==  IMAGE_SCALE_FITXY){
            rect.left = getPaddingLeft();
            rect.top = getPaddingTop();
            rect.right = mWidth- getPaddingRight();
            rect.bottom = mHeight - getPaddingBottom() - mTextBoundRect.height();
            canvas.drawBitmap(mImage,null,rect,mPaint);

        }else if(mImageScaleType == IMAGE_SCALE_CENTER){
            //计算居中的矩形范围
            rect.left = mWidth/2 - mImage.getWidth()/2;
            rect.right = mWidth/2+mImage.getWidth()/2;
            rect.top = (mHeight- mTextBoundRect.height())/2 - mImage.getHeight()/2;
            rect.bottom = (mHeight- mTextBoundRect.height())/2 + mImage.getHeight()/2;
            canvas.drawBitmap(mImage,null,rect,mPaint);

        }


        //在底部，水平中心画文字
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(mTitleTextColor);
        if(mTextBoundRect.width() > mWidth){

            //文字的宽度大于宽度，将多的字改成XXX...
            //截取文字，，把多出来的一部分替换成...
           String textstr = TextUtils.ellipsize(mTitleText,new TextPaint(mPaint),
                    mWidth-getPaddingLeft()-getPaddingRight(), TextUtils.TruncateAt.END).toString();
            canvas.drawText(textstr, getPaddingLeft(), mHeight-getPaddingBottom(), mPaint );

        }else {
            //正常，居中显示
            canvas.drawText(mTitleText, getMeasuredWidth()/2-mTextBoundRect.width()/2 , getMeasuredHeight()-getPaddingBottom() , mPaint);
        }


    }






}
